package main

import (
	"fmt"
	"math/rand"
	"sort"
	"strings"
	"time"
	"unicode"
)

//按照指定顺序遍历map
func traversalMap() {
	rand.Seed(time.Now().UnixNano())
	scoreMap := make(map[string]int, 200)
	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("student%d", i)
		value := rand.Intn(200)
		scoreMap[key] = value
	}
	//取出map中的所有key存入切片keys
	sliceKey := make([]string, 0, 200)
	for i := range scoreMap {
		sliceKey = append(sliceKey, i)
	}
	//切片排序
	sort.Strings(sliceKey)
	for _, i2 := range sliceKey {
		fmt.Println(scoreMap[i2])
	}
}

//元素为map类型的切片
func mapSlice() {
	var mapSlice = make([]map[string]string, 3)
	for index, value := range mapSlice {
		fmt.Printf("index:%d value:%v\n", index, value)
	}
	fmt.Println("after init")
	// 对切片中的map元素进行初始化
	mapSlice[0] = make(map[string]string, 10)
	mapSlice[0]["name"] = "小王子"
	mapSlice[0]["password"] = "123456"
	mapSlice[0]["address"] = "沙河"
	for index, value := range mapSlice {
		fmt.Printf("index:%d value:%v\n", index, value)
	}
}

//值为切片类型的map
func sliceMap() {
	var sliceMap = make(map[string][]string, 3)
	fmt.Println(sliceMap)
	fmt.Println("after init")
	key := "中国"
	value, ok := sliceMap[key]
	if !ok {
		value = make([]string, 0, 2)
	}
	value = append(value, "北京", "上海")
	sliceMap[key] = value
	fmt.Println(sliceMap)
}

//写一个程序，统计一个字符串中每个单词出现的次数。
func contWords(words string) {
	wordsSlice := strings.Split(words, " ")
	contMap := make(map[string]int, 15)
	for _, i2 := range wordsSlice {
		contMap[i2]++
	}
	fmt.Println(contMap)
}

//判断汉字出现的数量
func isChinese(str string) int {
	count := 0
	for _, i2 := range str {
		if unicode.Is(unicode.Han, i2) {
			count++
		}
	}
	return count
}
func main() {
	/*contWords("how do you do think you about")*/
	fmt.Println(isChinese("how do you do think you about拉到撒是"))
}
